// pages/find/find.js
//获取应用实例
var app = getApp()
var util = require('../../utils/util.js');
var commonService = require('../../services/commonService.js');
var activityService = require('../../services/activityService.js');
var userGroupService = require('../../services/userGroupService.js');
Page({
  data: {
    lives: 0,
    activitys: [],
    activityHasMore: true,//默认还有更多活动列表
    activityPageIndex: 1,
    activityPageSize: 10,

    groups: [],
    groupHasMore: true,//默认还有更多团队
    groupPageIndex: 1,
    groupPageSize: 10,
    groupName:'',

    pic_path: app.globalData.apiHost,
    search_status:false,
    search_focus:false
  },
  liveClick: function (e) {
    var that = this;
    this.data.search_status = false;
    if (this.data.lives === e.currentTarget.dataset.live) {
      return false;
    } else {
      that.setData({
        lives: e.currentTarget.dataset.live,
        search_status: this.data.search_status
      })
    }
  },
  clickDetails: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '../eventdetails/eventdetails?id=' + e.currentTarget.dataset.id
    });
  },
  releaseActivity: function (e) {
    this.getActivities(1);
  },
  //onLoad:function(options){
  // 页面初始化 options为页面跳转所带来的参数
  //},
  onLoad: function (opt) {
    util.checkPermission(this, opt);
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    var that = this;
    var today = new Date();
    that.data.activityHasMore = true;
    that.data.groupHasMore = true;
    that.data.activityPageIndex = 1;
    that.data.groupPageIndex = 1;
    if (!app.globalData.currentCityId) {
      app.updateLocation(function (city) {
        that.setData({
          activityPageIndex: 1,
          groupPageIndex: 1,
          cCityId: city.id
        })
        that.initCityId();
        that.getActivities();
        that.getGroups();
      });
    } else {
      that.initCityId();
      that.getActivities();
      that.getGroups();
    }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  getActivities: function (append) {
    var that = this;
    //是否继续加载，activityHasMore=false说明上一次查询之后没有获得更多数据了，因此不要再向服务器请求新的数据
    if (this.data.activityHasMore) {
      activityService.getActivityList({
        cityId: app.globalData.currentCityId,
        pageIndex: that.data.activityPageIndex,
        pageSize: that.data.activityPageSize,
        success: function (resultData) {
          var activities = [];

          if (append == 1) {
            activities = that.data.activities;
          }

          for (var i = 0; i < resultData.list.length; i++) {
            var data = resultData.list[i];
            // date.replace(/-/g, "/")
            data.start_time = util.makeDateTimeToPretty(new Date(data.active_time_start.replace(/-/g, "/")), true);
            data.end_time = util.makeDateTimeToPretty(new Date(data.active_time_end.replace(/-/g, "/")), true);
            data.remainJoinNum = data.volunteer_num - data.join_num;
            activities.push(resultData.list[i]);
          }

          that.setData({
            activities: activities,
            activityPageIndex: that.data.activityPageIndex + 1,
            activityHasMore: resultData.hasMore,
          });
        }
      });
    }
  },
  getGroups: function (append) {
    var that = this;
    //是否继续加载，groupHasMore=false说明上一次查询之后没有获得更多数据了，因此不要再向服务器请求新的数据
    if (this.data.groupHasMore) {
      userGroupService.getGroupList({
        cityId: app.globalData.currentCityId,
        pageIndex: that.data.groupPageIndex,
        pageSize: that.data.groupPageSize,
        group_name:that.data.groupName,
        success: function (resultData) {
          var groups = [];
          if (append == 1) {
            groups = that.data.groups;
          }
          //app.globalData.sysUserInfo.userId
          for (var i = 0; i < resultData.list.length; i++) {
            groups.push(resultData.list[i]);
          }
          for (var i = 0; i < groups.length; i++) {
            if (groups[i].groupUsers.length != 0) {
              for (var j = 0; j < groups[i].groupUsers.length; j++) {
                if (app.globalData.sysUserInfo.userId == groups[i].groupUsers[j]) {
                  groups[i].is_in = true;
                  break;
                } else {
                  groups[i].is_in = false;
                }
              }
            }
          }
          that.setData({
            groups: groups,
            groupPageIndex: that.data.groupPageIndex+1,
            groupHasMore: resultData.hasMore
          });
        }
      });
    }
  },
  initCityId: function () {
    var that = this
    activityService.getCityName({
      id: app.globalData.currentCityId,
      success: function (res) {
        if (res) {
          that.setData({
            cityName: res.name
          })
        }
      }
    })
  },
  userJoinGroupApply: function (opt) {
    var that = this;
    userGroupService.userJoinGroupApply({
      group_id: opt.group_id,
      user_id: opt.user_id,
      success: function (res) {
        wx.showToast({
          title: '申请成功!请耐心等待审核',
          icon: 'loading'
        })
      }
    })
  },
  applyJoin: function (e) {
    var that = this;
    that.userJoinGroupApply({
      group_id: e.currentTarget.dataset.id,
      user_id: app.globalData.sysUserInfo.userId
    })
  },
  releaseGroup:function(){
    this.getGroups(1);
  },
  clickSearch:function(){
    this.data.search_status = true;
    this.setData({
      search_status: this.data.search_status,
      search_focus:true
    });
  },
  doSearch:function(e){
    var that = this;
    that.setData({
      groupPageIndex: 1,
      groupHasMore: true,
      groupName: e.detail.value
    });
    that.getGroups();
  }
})